/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package labb6;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Lisa
 */
public class Bookmark extends JFrame implements ActionListener{
    private int count;
    private String currURL, setname;
    private JPanel panel1, panel2;
    private JTextField addname;
    private JTable tabel;
    private String[][] lista, templista;
    private String[] name = {"Name","Adress"};
    private JButton add, remove;
    private JScrollPane scroll;
    private LinkedHashMap map;
    
    Bookmark(){
       
       Container contentPane = this.getContentPane();
       contentPane.setLayout(new FlowLayout());
       panel1 = new JPanel(new FlowLayout()); 
       panel2 = new JPanel(new FlowLayout()); 
       
       add = new JButton("lägg till");
       add.setActionCommand("add");
       add.addActionListener(this);
       remove = new JButton("ta bort");
       remove.setActionCommand("remove");
       remove.addActionListener(this);
       
       addname = new JTextField(15);
       tabel = new JTable(50,2);
       scroll = new JScrollPane(tabel);
       contentPane.add(scroll);
       tabel.setPreferredScrollableViewportSize(new Dimension(200,150));
       
      
       panel1.add(addname);
       panel1.add(add);
       panel2.add(scroll);
       panel2.add(remove);
       
       
       contentPane.add(panel1);
       contentPane.add(panel2);
       setSize(400,300);
       setDefaultCloseOperation(this.HIDE_ON_CLOSE);
       count = 0;
       lista = new String[50][2];
       tabel.setModel(new DefaultTableModel(lista, name));
       ListSelectionModel sM = tabel.getSelectionModel();
       sM.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
       sM.addListSelectionListener(tabel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if  (e.getActionCommand().equals("add")){
            lista = addRow(lista);
            setname = addname.getText();
            lista[count][0] = setname;
            lista[count][1] = currURL;
            count++;
            lista = sortString(lista);
            tabel.setModel(new DefaultTableModel(lista, name));
            //System.out.println("BAJS"+count);
        }
        else if (e.getActionCommand().equals("remove")){
            int rowIndex = tabel.getSelectedRow();
            lista[rowIndex][0] = null;
            lista[rowIndex][1] = null;
            lista = removeRow(lista);
            count--;
            tabel.setModel(new DefaultTableModel(lista, name));
            
        }
    }
    public void newUrl(String url){
        currURL = url;
    }
    public String[][] sortString(String[][] string){
        Arrays.sort(string, new ArrayComp(0));
        return string;
    }
    public String[][] addRow(String[][] string){
        templista = string;
        string = new String[count+1][2];
        for (int j = 0; j<count; j++){
            for (int k = 0; k < 2; k++){
                    string[j][k] = templista[j][k];
                }
            }
        return string;
        
    }
    public String[][] removeRow(String[][] string){
        templista = string;
        int l = 0;
        string = new String[count-1][2];
        for (int j = 0; j<count; j++){
            for (int k = 0; k < 2; k++){
                if (templista[j][k] == null){
                    break;
                    }
                else {
                    string[l][k] = templista[j][k];
                    }
                }
             if (templista[j][0] != null){
                    l++;
                }
            }
        return string;
    }
}
